import { Body, Controller, Post, UsePipes, ParseIntPipe, Get, Param, Query, Put, Req} from '@nestjs/common';
import { isNumber } from 'class-validator';
import dtoValidationPipe from 'src/pipe/Dto.pipe';
import { RedisService } from 'src/redis/redis.service';
import { errorReturn, successReturn } from 'src/utils';
import { AddMerchantDto, EditMerchantDto, MerchantListDto } from './dto';
import { MerchantService } from './merchant.service';

@Controller('merchant')
export class MerchantController {
    constructor(
        private merchantService:MerchantService,
        private redisService:RedisService
    ){}

    @Get('list')
    @UsePipes(dtoValidationPipe({skipMissingProperties : true}))
    merchantList(@Query() dto:MerchantListDto) {
        return this.merchantService.merchantList(dto)
    }

    @Get('list/:name')
    merchantNameList(@Param('name') name:string, @Req() {roleType, userId}) {
        return this.merchantService.queryMerchantName(name, roleType, userId)
    }

    @Post('add')
    @UsePipes(dtoValidationPipe())
    async addMerchant(@Body() dto:AddMerchantDto) {
        let lockKey = ''
        try {
            if((await this.merchantService.accountIsExists(dto.merchantName, dto.account)) == true) return errorReturn('账号已经存在')
            lockKey = 'm:add:lock:' + dto.account
            const lock = await this.redisService.store.set(lockKey,'', {NX : true, EX : 10})
            if(!lock) return errorReturn()
            const id = await this.merchantService.add(dto)
            return successReturn()
        } catch (error) {
            
        }finally {
            this.redisService.del(lockKey)
        }

        return errorReturn()
    }

    @Get('edit/:merchantId')
    async getEditData(@Param('merchantId') merchantId:number) {
        merchantId = Number(merchantId)
        if(!isNumber(merchantId) || merchantId <= 0) return errorReturn()
        const data = await this.merchantService.getMerchantEditData(merchantId)
        return successReturn(data)
    }

    @Put('edit/:merchantId')
    @UsePipes(dtoValidationPipe({skipMissingProperties : true}))
    async editMerchant(@Param('merchantId') merchantId:number, @Body() dto:EditMerchantDto) {
        merchantId = Number(merchantId)
        if(!isNumber(merchantId) || merchantId <= 1) return errorReturn()
        try {
            const res = await this.merchantService.edit(merchantId, dto)
            if(res) {
                return successReturn()
            }
        } catch (error) {
            
        }

        return errorReturn()
    }

    @Put('enable/:merchantId')
    async enable(@Param('merchantId') merchantId:number) {
        merchantId = Number(merchantId)
        if(!isNumber(merchantId) || merchantId <= 1) return errorReturn()
        this.merchantService.updateMerchantStatus(merchantId, 1)
        return successReturn()
    }

    @Put('forbidden/:merchantId')
    async forbidden(@Param('merchantId') merchantId:number) {
        merchantId = Number(merchantId)
        if(!isNumber(merchantId) || merchantId <= 1) return errorReturn()
        if((await this.merchantService.updateMerchantStatus(merchantId, 0)) > 0) {
            return successReturn()
        }

        return errorReturn()
        
    }
}
